package Model;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Information.Admin;

public class AdminDAO{
	
	private int AdminNO;
	private String AdminUser;
	private String AdminPassword;
	private String AdminName;
	private String AdminEmail;
	
	public AdminDAO()
	{
		this.AdminNO=0;
		this.AdminUser="";
		this.AdminPassword="";
		this.AdminName="";
		this.AdminEmail = "";
	}
	
	public boolean AddNewAdmin(String AdminUser,String AdminPassword,String AdminName,String AdminEmail) throws SQLException, ClassNotFoundException
	{
		String sql ="insert into Admin (AdminUser,AdminPassword,AdminName,AdminEmail) values ('"+AdminUser+"','"+AdminPassword+"','"+AdminName+"','"+AdminEmail+"')";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/*
	public ArrayList<News> SeeNews(String NHeadline) throws SQLException, ClassNotFoundException
	{
		ArrayList<News> arrayList = null;
		News News = new News();
		String sql ="select * from News where NHeadline ='"+NHeadline+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		NHeadline = result.getString("NHeadline");
		NContent = result.getString("NContent");
		NImage = result.getString("NImage");
		NSource = result.getString("NSource");
		PublishTime = result.getDate("PublishTime");
		TotalClick = result.getInt("TotalClick");
		News.SetNewsInfo(NHeadline, NContent, NImage, NSource, PublishTime, TotalClick);
		arrayList.add(News);
		return arrayList;
	}*/
	
	public ArrayList<Admin> SeeAllNews() throws SQLException, ClassNotFoundException
	{
		ArrayList<Admin> arrayList = new ArrayList<Admin>();
		String sql ="select * from Admin";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		while(result.next())
		{
			Admin admin = new Admin();
			AdminNO = result.getInt("AdminNO");
			AdminUser = result.getString("AdminUser");
			AdminPassword = result.getString("AdminPassword");
			AdminName = result.getString("AdminName");
			AdminEmail = result.getString("AdminEmail");
			admin.SetAdminInfo(AdminNO, AdminUser, AdminPassword,AdminName, AdminEmail);
			arrayList.add(admin);
		}
		return arrayList;
	}
	
	public Admin FindAdmin(String username) throws SQLException, ClassNotFoundException
	{
		Admin admin = new Admin();
		String sql ="select * from Admin where AdminUser ='"+username+"'";
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		result = stmt.executeQuery(sql);
		if(result.next())
		{
			AdminNO= result.getInt("AdminNO");
			AdminUser = result.getString("AdminUser");
			AdminPassword = result.getString("AdminPassword");
			AdminName = result.getString("AdminName");
			AdminEmail = result.getString("AdminEmail");
		}
		admin.SetAdminInfo(AdminNO, AdminUser, AdminPassword, AdminName, AdminEmail);
		return admin;
	}
	
	public boolean DeleteAdmin(String NHeadline) throws ClassNotFoundException, SQLException
	{
		String sql ="delete from Admin where NHeadline='"+NHeadline+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean UpdateAdmin(String NHeadline,String NContent,String NImage,String NSource) throws ClassNotFoundException, SQLException
	{
		//can not update EUser
		String sql ="update Admin set NHeadline='"+NHeadline+"',set NContent='"+NContent+"',set NImage='"+NImage+"',set NSource='"+NSource+"'where NHeadline='"+NHeadline+"'";
		Connection con = null;
		Statement stmt = null;
		DatabaseConfig dConfig = new DatabaseConfig();
		con = dConfig.getConnection();
		stmt = con.createStatement();
		if(stmt.executeUpdate(sql)==1)
		{
			return true;//update successfully
		}
		else
		{
			return false;//update failed
		}
	}
}